package com.aier.user.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.ClassParent;
import com.aier.classes.entity.ClassStudent;
import com.aier.classes.entity.Classes;
import com.aier.classes.entity.Homework;
import com.aier.classes.entity.HomeworkCR;
import com.aier.classes.entity.HomeworkComment;
import com.aier.classes.entity.mapper.ClassParentRowMapper;
import com.aier.classes.entity.mapper.ClassStudentRowMapper;
import com.aier.classes.entity.mapper.ClassesRowMapper;
import com.aier.classes.entity.mapper.HomeworkCRRowMapper;
import com.aier.classes.entity.mapper.HomeworkCommentRowMapper;
import com.aier.classes.entity.mapper.HomeworkRowMapper;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.mapper.SchoolTeacherRowMapper;
import com.aier.user.entity.Appraise;
import com.aier.user.entity.AppraiseComment;
import com.aier.user.entity.DialogComment;
import com.aier.user.entity.User;
import com.aier.user.entity.mapper.AppraiseCommentRowMapper;
import com.aier.user.entity.mapper.AppraiseRowMapper;
import com.aier.user.entity.mapper.DialogCommentRowMapper;
import com.aier.user.entity.mapper.DialogRowMapper;
import com.aier.user.entity.mapper.UserRowMapper;
import com.aier.user.service.IHsConBookService;

@Component("hsConBookService")
public class HsConBookServiceImpl_J implements IHsConBookService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    @Override
    public int insertTDialog(Long cid, Long tid, String buidStr, String content) {
        for (String buid:buidStr.split(",")) {
            if ("-1".equals(buid)) {
                continue;
            }
            String headimg = (String)commonDao.listByObject(
                    "SELECT u.headimg FROM `user` u LEFT JOIN school_teacher st on st.uid = u.uid WHERE st.tid = ?", 
                    new Object[]{tid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("headimg");
                        }
                    }
            );
            commonDao.update("insert into dialog(tid,buid,cid,headimg,content,cdate,type) values(?,?,?,?,?,?,?)",
                    new Object[]{tid,buid,cid,headimg,content,new Timestamp(new Date().getTime()),1});
        }
        
        return 1;
    }

    @Override
    public int insertPDialog(Long cid, String tidStr, Long puid, Long buid,
            String content) {
        for (String tid:tidStr.split(",")) {
            if ("-1".equals(tid)) {
                continue;
            }
            String headimg = (String)commonDao.listByObject(
                    "SELECT u.headimg FROM `user` u WHERE u.uid = ?", 
                    new Object[]{puid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("headimg");
                        }
                    }
            );
            commonDao.update("insert into dialog(tid,buid,puid,cid,headimg,content,cdate,type) values(?,?,?,?,?,?,?,?)",
                    new Object[]{tid,buid,puid,cid,headimg,content,new Timestamp(new Date().getTime()),2});
        }
        return 1;
    }

    @Override
    public int insertCommentDialog(Long uid, Long did, String content,Long tid,Short type) {
        String cname = "";
        if (type == 1) {
            
            cname = (String)commonDao.listByObject(
                    "SELECT st.tname as cname from school_teacher st WHERE st.tid = ?", 
                    new Object[]{tid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            )+"老师";
        } else {
            cname = (String)commonDao.listByObject(
                    "select concat(cs.sname,'的',pb.relation) as cname from dialog d left join parent_baby pb on pb.baby_uid = d.buid " +
                    "left join class_student cs on cs.uid = d.buid where d.did = ? and pb.parent_uid = ?", 
                    new Object[]{did,uid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            );
        }
        return commonDao.update("insert into dialog_comment(did,cuid,cname,content,cdate) values(?,?,?,?,?)",
                    new Object[]{did,uid,cname,content,new Timestamp(new Date().getTime())});
        
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<DialogComment> searchDCByDid(Long did) {
        String sql = "select dc.*,u.headimg from dialog_comment dc left join user u on u.uid = dc.cuid where dc.did = ? order by dc.cdate desc";
        return (List<DialogComment>) commonDao.list(sql,new Object[]{did},new DialogCommentRowMapper());
    }

    @Override
    public Map<String, Object> searchPDialogList(int page, int pagesize, Long buid) {
        String sql = "select d.*, st.tname, us.nickname as bname, pb.relation from dialog d left join school_teacher st on st.tid = d.tid " +
        		"left join `user` us on us.uid = d.buid left join parent_baby pb " +
        		"on pb.baby_uid = d.buid and pb.parent_uid = d.puid where d.buid = ? order by d.cdate desc";
        return commonDao.queryPage(sql, page, pagesize, new Object[]{buid}, new DialogRowMapper());
    }

    @Override
    public Map<String, Object> searchTDialogList(int page, int pagesize,
            Long cid,Long tid) {
        String sql = "select d.*, st.tname, us.nickname as bname, pb.relation from dialog d left join school_teacher st on st.tid = d.tid " +
        		"left join `user` us on us.uid = d.buid left join parent_baby pb on pb.baby_uid = d.buid and pb.parent_uid = d.puid " +
        		"where d.cid = ? and d.tid = ? order by d.cdate desc";
        return commonDao.queryPage(sql, page, pagesize, new Object[]{cid,tid}, new DialogRowMapper());
    }

    @Override
    public long insertAppraise(Long cid, Long tid, String bstr, String content) {
        Appraise ap = new Appraise();
        ap.setCid(cid);
        ap.setTid(tid);
        ap.setContent(content);
        ap.setBuid(Long.valueOf(bstr));
        ap.setCdate(new Timestamp(new Date().getTime()));
        KeyHolder kh = commonDao.insert(AppraiseRowMapper.getPreparedStatementCreator(ap), new GeneratedKeyHolder());
        return kh.getKey().longValue();
        
//        for (String buid:buidStr.split(",")) {
//            if ("-1".equals(buid)) {
//                continue;
//            }
//            commonDao.update("insert into appraise(tid,buid,cid,content,cdate) values(?,?,?,?,?)",
//                    new Object[]{tid,buid,cid,content,new Timestamp(new Date().getTime())});
//        }
//        return 1;
    }

    @Override
    public int insertCommentAppraise(Long uid, Long apid, String content,Long tid,
            Short type) {
        String cname = "";
        if (type == 1) {
            
            cname = (String)commonDao.listByObject(
                    "SELECT st.tname as cname from school_teacher st WHERE st.tid = ?", 
                    new Object[]{tid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            )+"老师";
        } else {
            cname = (String)commonDao.listByObject(
                    "select concat(cs.sname,'的',pb.relation) as cname from appraise a left join parent_baby pb on pb.baby_uid = a.buid " +
                    "left join class_student cs on cs.uid = a.buid where a.apid=? and pb.parent_uid = ?", 
                    new Object[]{apid,uid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            );
        }
        return commonDao.update("insert into appraise_comment(apid,cuid,cname,content,cdate) values(?,?,?,?,?)",
                    new Object[]{apid,uid,cname,content,new Timestamp(new Date().getTime())});
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<AppraiseComment> searchACByDid(Long apid) {
        String sql = "select ac.*,u.headimg from appraise_comment ac left join user u on u.uid = ac.cuid where apid = ? order by cdate desc";
        return (List<AppraiseComment>) commonDao.list(sql,new Object[]{apid},new AppraiseCommentRowMapper());
    }

    @Override
    public Map<String, Object> searchPAppraiseList(int page, int pagesize,
            Long buid) {
        String sql = "select a.*, st.tname, u.headimg from appraise a " +
                "left join school_teacher st on st.tid = a.tid left join `user` u " +
                "on u.uid = st.uid where a.buid = ? order by a.cdate desc";
        return commonDao.queryPage(sql, page, pagesize, new Object[]{buid}, new AppraiseRowMapper());
    }

    @Override
    public Map<String, Object> searchTAppraiseList(int page, int pagesize,
            Long cid, Long tid) {
        
        String sql = " from appraise a left join school_teacher st on st.tid = a.tid " +
                "left join `user` u on u.uid = st.uid left join class_student cs on cs.uid = a.buid where a.cid = ?";
        List<Object> largcount = new ArrayList<Object>();
        List<Object> largcount1 = new ArrayList<Object>();
        largcount.add(cid);
        largcount.add(cid);
        largcount.add(cid);
        largcount1.add(cid);
        if (tid != null) {
            sql += " and a.tid = ?";
            largcount.add(tid);
            largcount1.add(tid);
        }
        sql += " order by a.cdate desc";
        return commonDao.queryPage("select a.*, st.tname, u.headimg, cs.sname as bname,(select GROUP_CONCAT(DISTINCT(cp.pname)) from " +
        		"school_phonemessage_history sph left join class_parent cp on sph.uid = cp.uid where type = 4 and relativeid = a.apid " +
        		"and cp.cid = ?) as successStr,(select GROUP_CONCAT(DISTINCT(cp.pname)) from phonemessage_sendError_history psh left join " +
        		"class_parent cp on psh.uid = cp.uid where type = 4 and relativeid = a.apid and cp.cid = ?) as failStr"+sql,
        		"select count(*)"+sql, page, pagesize, largcount,largcount1, new AppraiseRowMapper(AppraiseRowMapper.AND_MESSAGE));
    }

    @Override
    public long insertHomework(Long cid, Long tid,String title, String content, String img,Integer count) {
        
        Homework hw = new Homework();
        hw.setCid(cid);
        hw.setTid(tid);
        hw.setTitle(title);
        hw.setContent(content);
        hw.setImg(img);
        hw.setCdate(new Timestamp(new Date().getTime()));
        hw.setCount(count);
        KeyHolder kh = commonDao.insert(HomeworkRowMapper.getPreparedStatementCreator(hw), new GeneratedKeyHolder());
        return kh.getKey().longValue();
    }

    @Override
    public HomeworkComment insertHComment(Long uid, Long hid, String content, String img, Short type,Long buid,Long tid) {
        String cname = searchRname(uid, buid, type,tid);
        HomeworkComment hc = new HomeworkComment();
        hc.setUid(uid);
        hc.setHid(hid);
        hc.setContent(content);
        hc.setImg(img);
        hc.setCname(cname);
        hc.setCdate(new Timestamp(new Date().getTime()));
        KeyHolder kh = commonDao.insert(HomeworkCommentRowMapper.getPreparedStatementCreator(hc),new GeneratedKeyHolder());
        return (HomeworkComment)commonDao.listByObject("select hc.*,u.headimg from homework_comment hc left join `user` u on u.uid = hc.uid " +
                "where hc.hcid = ?",
                new Object[]{kh.getKey().longValue()},new HomeworkCommentRowMapper());
    }

    @Override
    public HomeworkCR insertReply(Long uid, Long chid, String content, Short type, Long buid,Long tid) {
        String cname = searchRname(uid, buid, type,tid);
        HomeworkCR hcr = new HomeworkCR();
        hcr.setChid(chid);
        hcr.setUid(uid);
        hcr.setCname(cname);
        hcr.setContent(content);
        hcr.setCdate(new Timestamp(new Date().getTime()));
        KeyHolder kh = commonDao.insert(HomeworkCRRowMapper.getPreparedStatementCreator(hcr),new GeneratedKeyHolder());
        return (HomeworkCR) commonDao.listByObject("select hcr.*,u.headimg from homework_comment_reply hcr left join `user` u " +
        		"on u.uid = hcr.uid where hcr.crhid = ?", 
                new Object[]{kh.getKey().longValue()},new HomeworkCRRowMapper());
        
    }
    
    private String searchRname(Long uid,Long buid,Short type,Long tid) {
        String cname = "";
        if (type == 1) {
            cname = (String)commonDao.listByObject(
                    "SELECT st.tname as cname from school_teacher st WHERE st.tid = ?", 
                    new Object[]{tid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            )+"老师";
        } else {
            cname = (String)commonDao.listByObject(
                    "select concat(cs.sname, '的', pb.relation) as cname from class_student cs left join parent_baby pb on pb.baby_uid = cs.uid " +
                    "where pb.parent_uid = ? and pb.baby_uid = ?", 
                    new Object[]{uid,buid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            );
        }
        return cname;
    }

    @Override
    public Map<String, Object> searchPHomework(int page, int page_size,
            Long buid) {
        return commonDao.queryPage("select h.*, st.tname,u.headimg from homework h left join class_student cs on h.cid = cs.cid " +
        		"left join school_teacher st on st.tid = h.tid left join `user` u on u.uid = st.uid where cs.uid = ? order by h.cdate desc", 
                page, page_size, new Object[]{buid}, new HomeworkRowMapper());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<HomeworkComment> searchHCList(Long hid) {
        return (List<HomeworkComment>) commonDao.list("select hc.*,u.headimg from homework_comment hc left join `user` u on u.uid = hc.uid " +
        		"where hc.hid = ?",
                new Object[]{hid},new HomeworkCommentRowMapper());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<HomeworkCR> searchHCRList(Long chid) {
        return (List<HomeworkCR>) commonDao.list("select hcr.*,u.headimg from homework_comment_reply hcr left join `user` u on u.uid = hcr.uid " +
        		"where hcr.chid = ?",
                new Object[]{chid},new HomeworkCRRowMapper());
    }

    @Override
    public Map<String, Object> searchTHomework(int page, int page_size, Long cid) {
        String sql = "from homework h left join school_teacher st on st.tid = h.tid left join `user` u on u.uid = st.uid where cid = ? " +
        		"order by h.cdate desc";
        return commonDao.queryPage("select h.* ,st.tname,u.headimg,(select GROUP_CONCAT(DISTINCT(cp.pname)) from school_phonemessage_history sph " +
        		"left join class_parent cp on sph.uid = cp.uid where type = 3 and relativeid = h.hid and cp.cid = ?) as successStr," +
        		"(select GROUP_CONCAT(DISTINCT(cp.pname)) from phonemessage_sendError_history psh left join class_parent cp " +
        		"on psh.uid = cp.uid where type = 3 and relativeid = h.hid and cp.cid = ?) as failStr "+sql, "select count(*) "+sql,
                page, page_size, new Object[]{cid,cid,cid},new Object[]{cid}, new HomeworkRowMapper(HomeworkRowMapper.AND_MESSAGE));
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SchoolTeacher> searchSTByUid(Long uid) {
        return (List<SchoolTeacher>) commonDao.list(
                "select *,(s.name) as name_school from school_teacher st left join school s on st.sid=s.sid where st.uid = ? and st.isadmin = 1", 
        		new Object[]{uid}, new SchoolTeacherRowMapper(SchoolTeacherRowMapper.AND_SCHOOLNAME));
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Classes> searchTClasses(Long uid) {
        return (List<Classes>) commonDao.list("select c.*,ct.tid from classes c LEFT JOIN class_teacher ct on c.cid = ct.cid " +
        		"LEFT JOIN school_teacher st on st.tid = ct.tid where st.uid = ?", 
                new Object[] { uid },new ClassesRowMapper(ClassesRowMapper.AND_TID));
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ClassStudent> searchCTByUid(Long uid) {
        String sql = "select cs.* from class_student cs left join parent_baby pb on pb.baby_uid = cs.uid where pb.parent_uid=?";
        return (List<ClassStudent>) commonDao.list(sql,new Object[] { uid },new ClassStudentRowMapper());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> searchBabyByUid(Long uid) {
        return (List<User>) commonDao.list("select u.*, cs.cid as baby_classid, (select cname from classes where cid = cs.cid) " +
        		"as baby_classname from class_student cs left join parent_baby pb on cs.uid = pb.baby_uid " +
        		"left join user u on u.uid = pb.baby_uid where pb.parent_uid = ?",
                new Object[]{uid},new UserRowMapper(UserRowMapper.BABY_CLASSNAMEANDID));
    }

    @Override
    public int searchMessageSuccessCount(Long mid) {
        String str = (String)commonDao.listByObject(
                "select count(*) as count from school_phonemessage_history where type = 1 and relativeid = ?", 
                new Object[]{mid},new RowMapper<String>() {
                    @Override
                    public String mapRow(ResultSet rs, int arg1)
                        throws SQLException {
                        return rs.getString("count");
                    }
                }
        );
        return Integer.valueOf(str);
    }

    @Override
    public String searchMessageFailStr(Long mid) {
        return (String)commonDao.listByObject("select group_concat(distinct(cp.pname)) as failstr from phonemessage_sendError_history psh " +
        		"left join class_parent cp on psh.uid = cp.uid where psh.type = 1 and psh.relativeid = ?", 
                new Object[]{mid},new RowMapper<String>() {
                    @Override
                    public String mapRow(ResultSet rs, int arg1)
                        throws SQLException {
                        return rs.getString("failStr");
                    }
                }
        );
    }
    
    
    @Override
    public int deleteMessage(Long mid) {
        return commonDao.update("delete from school_message where mid = ?",new Object[]{mid});
    }
    
    @Override
    public Map<String, Object> searchHomework(int page, int page_size, Long cid) {
        String sql = "from homework h left join school_teacher st on st.tid = h.tid where cid = ? order by h.cdate desc";
        return commonDao.queryPage("select h.* ,st.tname "+sql, "select count(*) "+sql,
                page, page_size, new Object[]{cid},new Object[]{cid}, new HomeworkRowMapper());
    }

    @Override
    public Homework searchHomework(Long hid) {
        return (Homework) commonDao.listByObject("select h.*,st.tname,u.headimg from homework h left join school_teacher st on st.tid = h.tid " +
        		"left join `user` u on u.uid = st.uid where hid = ?", new Object[]{hid},new HomeworkRowMapper());
    }

    @Override
    public String searchUnReadingList(Long hid) {
        String snameList = (String)commonDao.listByObject("select  group_concat(cs.sname) as snameList from class_student cs " +
                "left join homework h on cs.cid = h.cid where cs.csid not in (select csid from homework_read hr " +
                "where hr.hid = ?) and h.hid = ?", 
                new Object[]{hid,hid},
                new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet rs, int arg1)
                    throws SQLException {
                    return rs.getString("snameList");
                }
            }
        );
        return snameList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ClassStudent> searchCSByCondition(Long hid) {
        List<ClassStudent> csList = (List<ClassStudent>) commonDao.list("select cs.* from class_student cs LEFT JOIN homework_read hr " +
                "on cs.csid = hr.csid where hr.hid = ?",new Object[]{hid},new ClassStudentRowMapper());
        for (ClassStudent cs:csList) {
            cs.setCpList((List<ClassParent>)commonDao.list("select * from class_parent cp LEFT JOIN homework_read hr on cp.cpid = hr.cpid " +
                    "where hr.hid = ? and hr.csid = ?",new Object[]{hid,cs.getCsid()},new ClassParentRowMapper()));
        }
        return csList;
    }

    @Override
    public int deleteComment(Long chid) {
        return commonDao.update("delete from homework_comment where chid = ?",new Object[]{chid});
    }

    @Override
    public int deleteReply(Long crhid) {
        return commonDao.update("delete from homework_comment_reply where crhid = ?",new Object[]{crhid});
    }
    
    @SuppressWarnings("rawtypes")
    @Override
    public int insertHomeworkReading(Long hid,Long uid) {
        // 家长阅读
        Homework h = (Homework)commonDao.listByObject("select * from homework where hid = ?", 
                new Object[]{hid},new HomeworkRowMapper());
        @SuppressWarnings("unchecked")
        List<ClassParent> cpList = (List<ClassParent>) commonDao.list("select * from class_parent where cid = ? and uid = ?",
                new Object[]{h.getCid(),uid});
        
        String count = (String)commonDao.listByObject("select count(*) as count from homework_read where hid = ? and cpid = ?", 
                new Object[]{hid,((Map)cpList.get(0)).get("cpid")},
                new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet rs, int arg1)
                    throws SQLException {
                    return rs.getString("count");
                }
            }
        );
        if ("0".equals(count)) {
            commonDao.update("update homework set reading = IFNULL(reading,0)+? where hid=?",
                    new Object[] {cpList.size(), hid});
            for(int i=0;i<cpList.size();i++) {
                commonDao.update("insert into homework_read(hid,csid,cpid) values(?,?,?)",
                        new Object[] {hid,((Map)cpList.get(i)).get("csid"),((Map)cpList.get(i)).get("cpid")});
            }
            
        }
            
        return 1;
    }
    
    @Override
    public HomeworkComment insertHComment(Long uid, Long hid, String content, String img, Short type,Long tid) {
        Homework h = (Homework) commonDao.listByObject("select * from homework where hid = ?", new Object[]{hid},new HomeworkRowMapper());
        String cname = searchName(uid, h.getCid(), type,tid);
        HomeworkComment hc = new HomeworkComment();
        hc.setUid(uid);
        hc.setHid(hid);
        hc.setContent(content);
        hc.setImg(img);
        hc.setCname(cname);
        hc.setCdate(new Timestamp(new Date().getTime()));
        KeyHolder kh = commonDao.insert(HomeworkCommentRowMapper.getPreparedStatementCreator(hc),new GeneratedKeyHolder());
        return (HomeworkComment)commonDao.listByObject("select hc.*,u.headimg from homework_comment hc left join `user` u on u.uid = hc.uid " +
                "where hc.chid = ?",
                new Object[]{kh.getKey().longValue()},new HomeworkCommentRowMapper());
    }
    
    @Override
    public HomeworkCR insertReply(Long uid, Long chid, String content, Short type, Long tid) {
        Homework h = (Homework) commonDao.listByObject("select h.* from homework h left join homework_comment hc on h.hid = hc.hid " +
        		"where hc.chid = ?", new Object[]{chid},new HomeworkRowMapper());
        String hcname=searchHCName(h.getHid(),chid);
        String cname = searchName(uid, h.getCid(), type,tid);
        HomeworkCR hcr = new HomeworkCR();
        hcr.setChid(chid);
        hcr.setUid(uid);
        hcr.setCname(cname+" 回复了 "+ hcname );
        hcr.setContent(content);
        hcr.setCdate(new Timestamp(new Date().getTime()));
        KeyHolder kh = commonDao.insert(HomeworkCRRowMapper.getPreparedStatementCreator(hcr),new GeneratedKeyHolder());
        return (HomeworkCR) commonDao.listByObject("select hcr.*,u.headimg from homework_comment_reply hcr left join `user` u " +
                "on u.uid = hcr.uid where hcr.crhid = ?", 
                new Object[]{kh.getKey().longValue()},new HomeworkCRRowMapper());
        
    }
    
    private String searchName(Long uid,Long cid,Short type,Long tid) {
        String cname = "";
        if (type == 1) {
            cname = (String)commonDao.listByObject(
                    "SELECT st.tname as cname from school_teacher st WHERE st.tid = ?", 
                    new Object[]{tid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            )+"老师";
        } else {
            cname = (String)commonDao.listByObject(
                    "select concat(group_concat(cs.sname),'的', pb.relation) as cname from class_student cs left join parent_baby pb " +
                    "on pb.baby_uid = cs.uid where pb.parent_uid = ? and cs.cid = ?", 
                    new Object[]{uid,cid},new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("cname");
                        }
                    }
            );
        }
        return cname;
    }

	@Override
	public String  searchHCName(Long hid, Long chid) {
		HomeworkComment hc= (HomeworkComment) commonDao.listByObject("select hc.*,u.headimg from homework_comment hc left join `user` u on u.uid = hc.uid " +
        		"where hc.hid = ? and chid =? ",
                new Object[]{hid,chid},new HomeworkCommentRowMapper());
		
		String hcname= (String) commonDao.listByObject("select pname from class_parent  where uid = "+hc.getUid(), new RowMapper<String>(){
			  @Override
              public String mapRow(ResultSet rs, int arg1)
                  throws SQLException {
                  return rs.getString("pname");
			  }
		});
		return hcname;
	}
    
}
